﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System;

namespace CodeCamp.BL.Helpers
{
	public static class ControllerHelper_
	{
		
		public const string MESSAGE_VIEW_DATA_KEY = "Message";

		public static void ShowMessage(this Controller controller, string text)
		{
			controller.ViewData[MESSAGE_VIEW_DATA_KEY] = text;
		}

		public static void ShowError(this Controller controller, string text)
		{
			controller.ModelState.AddModelError("?", text);
		}

		public static bool IsValidUploadedFile(this HttpPostedFileBase uploadedFile, string[] allowedTypes, int maxSize)
		{
			if (uploadedFile == null)
			{
				return false;
			}

			string extension = Path.GetExtension(uploadedFile.FileName);
			if (extension == string.Empty)
			{
				return false;
			}

			if (!allowedTypes.Any(t => t == uploadedFile.ContentType.ToLower()))
			{
				return false;
			}

			if (uploadedFile.ContentLength > maxSize)
			{
				return false;
			}

			if (uploadedFile.ContentLength <= 8)
			{
				return false;
			}

			return true;
		}

	}
}